package com.pingan.glue.gluegame.controller;

import com.alibaba.fastjson.JSONObject;
import com.pingan.glue.gluegame.aip.APIService;
import com.pingan.glue.gluegame.aip.AuthService;
import com.pingan.glue.gluegame.aip.domain.FaceAddDto;
import com.pingan.glue.gluegame.aip.domain.FaceSearchDto;
import com.pingan.glue.gluegame.common.Constant;
import com.pingan.glue.gluegame.common.utils.FileUtils;
import com.pingan.glue.gluegame.response.ResponseData;
import com.pingan.glue.gluegame.response.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/demo")
public class DemoController {
    private final static Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

    @Value("${web.upload-path}")
    private String path;

    @Resource
    private AuthService authService;
    @Resource
    @Qualifier("baidu")
    private APIService apiService;

    @GetMapping()
    public ResponseResult<String> demo(){
        return new ResponseResult(new ResponseData(authService.getAuth()));
    }

    /**
     *
     * @param file 要上传的文件
     * @return
     */
    @PostMapping("fileUpload")
    public ResponseEntity<String> upload(@RequestParam("fileName") MultipartFile file, @RequestParam("userId") String userId){

        Map<String,Object> map =new HashMap<>();
        // 上传成功或者失败的提示
        String msg ;
        String image = FileUtils.upload(file, path+file.getOriginalFilename());
        if (image ==null){
            msg = "上传失败";
        }else{
            FaceAddDto dto = new FaceAddDto();
            dto.setImage(image);
            dto.setGroup_id("group_repeat");
            dto.setUser_id(userId);
            dto.setUser_info("abc");
            dto.setImage_type(Constant.IMAGES_TYPE_BASE64);
            msg = JSONObject.toJSONString(apiService.add(dto));

        }

        // 显示图片
        map.put("msg", msg);
        map.put("fileName", file.getOriginalFilename());

        return new ResponseEntity(JSONObject.toJSONString(map), HttpStatus.OK);
    }

    @PostMapping("detect")
    public ResponseEntity<String> detect(@RequestParam("fileName") MultipartFile file){

        Map<String,Object> map =new HashMap<>();
        // 上传成功或者失败的提示
        String msg ;
        String image = FileUtils.upload(file, path+file.getOriginalFilename());
        if (image ==null){
            msg = "上传失败";
        }else{
            msg = JSONObject.toJSONString(apiService.detect(image));
        }

        // 显示图片
        map.put("msg", msg);
        map.put("fileName", file.getOriginalFilename());

        return new ResponseEntity(JSONObject.toJSONString(map), HttpStatus.OK);
    }

    @PostMapping("search")
    public ResponseEntity<String> search(@RequestParam("fileName") MultipartFile file,String groupIds){

        Map<String,Object> map =new HashMap<>();
        // 上传成功或者失败的提示
        String msg ;
        String image = FileUtils.upload(file, path+file.getOriginalFilename());
        if (image ==null){
            msg = "上传失败";
        }else{
            FaceSearchDto dto = new FaceSearchDto();
            dto.setImage(image);
            dto.setImage_type(Constant.IMAGES_TYPE_BASE64);
            dto.setGroup_id_list("group_repeat");
            dto.setMax_user_num("20");
            msg = JSONObject.toJSONString(apiService.search(dto));
        }

        // 显示图片
        map.put("msg", msg);
        map.put("fileName", file.getOriginalFilename());

        return new ResponseEntity(JSONObject.toJSONString(map), HttpStatus.OK);
    }


}
